/**
 * 产品成本价格维护
 * @author shixc
 * @date 2019/01/16
 */
define(function () {

    var self,
        doc = document,
        app = win.app,
        layui = win.layui,
        element,
        table,
        table_gwax = {},
        cols = [[
            {field: 'ID', type: 'checkbox', title: '选择', fixed: 'left'}
            , {field: 'GWER001', width: 100, title: '产品编码', fixed: 'left', sort: true,
                search: 'default',searchOpts:{icon:true}}
            , {field: 'GWAML003', width: 200, title: '品名',sort:'default'}
            , {field: 'GWAML004', width: 80, title: '规格'}
            , {field: 'GWER002', width: 100, title: '币种'}
            , {field: 'GWER003', width: 100, title: '交易单位'}
            , {field: 'GWER004', width: 120, title: '成本归属年月'}
            , {field: 'GWER005', width: 120, title: '实际人工成本'}
            , {field: 'GWER006', width: 120, title: '实际材料成本'}
            , {field: 'GWER007', width: 120, title: '实际水电成本'}
            , {field: 'GWER008', width: 120, title: '实际折旧成本'}
            , {field: 'GWER009', width: 120, title: '实际其他成本'}
            , {field: 'GWER010', width: 120, title: '实际期间费用'}
            , {field: 'GWER012', width: 80, title: '备注'}
            , {
                field: 'GWERACTI', width: 80, title: '有效否',
                filterDatas: app.datas_('sys_y_n')
            },
            {
                width: 60,
                title: '',
                toolbar: '#script-gwei023setting',
                fixed: 'right',
                align: 'center',
                unresize: true
            }
        ]],
        gwei023SettingAction
    ;

    var Gwei023 = app.Module.extend({
        init: function () {
            self = this;
            self._super({
                _tag: 'gwei023',
                _searchs: [{
                    id: 'gwei023', tableId: 'gwei023_table', datas: cols[0]
                }],
                _loadTplsFull: [{
                    dom: 'gwei023_edit', tpl: '/gwei023_edit'
                },{
                    dom: 'common_open_window', tpl: 'common/_open_window'
                }]
            }, win);
        },

        initTable: function () {
            layui.use(['table'], function () {
                table = layui.table;
                table.render({
                    elem: app.getByid(doc, 'gwei023_table'),
                    url: g_ctxPath + 'crm/gwei023/getList',
                    exportsUrl: g_ctxPath + 'crm/gwei023/exportsData',
                    exportsName: '产品成本价格数据',
                    title: '产品成本价格信息表',
                    method: 'post',
                    id: 'gwei023_table',
                    size: _layuiConfig.ui_size,
                    autoSort: false, //禁用前端自动排序
                    toolbar: '#table-toolbar',
                    defaultToolbar: ['filter', 'exports'],
                    height: 'full',
                    cols: cols,
                    loading: true,
                    page: true,
                    where: '',
                    done: function (res, curr, count) {

                    }
                });

                layui.laytpl(app.getByid(doc, 'script-gwei023setting-action').innerHTML).render({}, function (string) {
                    gwei023SettingAction = string;
                });

                var tipsIndex = null;

                table.on('tool(gwei023_table)', function (obj) {

                    layer.tips(gwei023SettingAction, obj.td, {
                        tips: [3, '']
                        , time: 3000
                        , anim: 500
                        , maxWidth: 120
                        , skin: 'layui-table-tips'
                        , success: function (layero, index) {
                            layero.find('li').on('click', function () {
                                switch ($(this).attr('inx')) {
                                    case '0':
                                        self._openTplFull({
                                            tag: 'gwei023_edit',
                                            tagData: obj,
                                            index: 0,
                                            openOpts: {
                                                title: '产品成本价格编辑',
                                                area: ['720px', '500px']
                                            }
                                        });
                                        break;
                                    case '1':
                                        self.del(obj);
                                        break;
                                }
                                layer.close(index);
                            });
                        }
                    });
                });

                //监听行双击事件
                table.on('rowDouble(gwei023_table)', function (obj) {
                    self._openTplFull({
                        tag: 'gwei023_edit',
                        tagData: obj,
                        index: 0,
                        openOpts: {
                            title: '产品成本价格编辑',
                            area: ['720px', '500px']
                        }
                    });
                });

                //监听事件
                table.on('toolbar(gwei023_table)', function (obj) {
                    switch (obj.event) {
                        case 'add':
                            self._openTplFull({
                                tag: 'gwei023_edit',
                                tagData: {
                                    is_add: true
                                },
                                openOpts: {
                                    title: '产品成本价格新增',
                                    area: ['720px', '500px']
                                },
                                index: 0
                            });
                            break;
                        case 'del':
                            app.confirm({
                                msg: '是否删除选择的数据？',
                                ok: function () {
                                    self.deleteDataBatch();
                                }
                            });
                            break;
                    }
                });

                $(doc).on('click', function () {
                    tipsIndex && layer.close(tipsIndex);
                });

            });
        },

        reloadTableCurr: function () {
            self._reloadTable({id: 'gwei023_table', where: table_gwax});
        },

        del: function (obj) {
            app.confirm({
                msg: '是否删除选择的信息？',
                ok: function () {
                    app.ajax({
                        xurl: 'crm/gwei023/del',
                        xdata: obj.data,
                        loading: true,
                        okmsg: '产品成本价格信息删除成功',
                        cb: function (result) {
                            if (result.ok) {
                                self.reloadTableCurr();
                            }
                        }
                    });
                }
            });

        },

        deleteDataBatch: function () {
            var checkStatus = table.checkStatus('gwei023_table');
            if (checkStatus.data.length > 0) {
                var _xdata = [];
                checkStatus.data.forEach(function (v) {
                    var _temp = app.fields_('GWER');
                    _temp.GWERENT = v.GWERENT;
                    _temp.GWER001 = v.GWER001;
                    _temp.GWER004 = v.GWER004;
                    _temp.GWERPLANT = v.GWERPLANT;
                    _xdata.push(_temp);
                });
                app.ajax({
                    xurl: 'crm/gwei023/deleteDataBatch',
                    xdata: _xdata,
                    loading: true,
                    toJsonStr: true,
                    okmsg: '产品成本价格信息删除成功',
                    cb: function (result) {
                        if (result.ok) {
                            self.reloadTableCurr();
                        }
                    }
                });
            }
        },

        _ready: function () {
            this._super({
                vue: [{
                    el: '#page',
                    data: '',
                    searchId: 'gwei023',
                    methods: {
                        handleIconClick: function (field) {
                            var vue = this;
                            self._openTplFull({
                                tag: 'common_open_window',
                                tagData: {
                                    config: {
                                        method:'q_gwam001',
                                        columnList: [
                                            {label: '产品编号', prop: 'GWAM001', width: '150'},
                                            {label: '产品名称', prop: 'GWAML003', width: '150'},
                                            {label: '规格', prop: 'GWAML004', width: '150'}
                                        ]
                                    },
                                    cb: function (data) {
                                        vue.isearch.fields.GWER001 = data.GWAM001;
                                    },
                                },
                                index: 1,
                                openOpts: {
                                    title: '选择产品',
                                    area: ['600px', '465px']
                                }
                            });
                        }
                    }
                }]
            });
            self.initTable();
        },
        _lazy: function () {
            this._super();
            layui.use(['table'], function () {
                element = layui.element;
            });
        }
    });

    return new Gwei023();

});